﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleExpressionEvaluator;
using SimpleExpressionEvaluator.Evaluation;
using SimpleServiceBus.Bus;

namespace SimpleServiceBus.Utilities.ExpressionEval
{
    public class MessageExpressionEvaluator : IMessageExpressionEvaluator
    {
        private readonly ExpressionEvaluator _evaluator = new ExpressionEvaluator();

        public T Evaluate<T>(string expression, MessageEnvelope message)
        {
            var ctx = new ExpressionContext(message.Body);
            ctx.ResolveUnknownVariable += (sender, args) =>
                                              {
                                                  args.VariableValue = message.Headers[args.VariableName];
                                              };
            return _evaluator.EvaluateExpression<T>(expression, ctx);
        }
    }
}
